using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MessageLibrary
{
    [Serializable]
    public class LogInMessage : AbsMessage
    {
        /// <summary>
        /// A message that informs both client and
        /// server that a client has logged in
        /// </summary>
        /// <param name="name">client's user name</param>
        public LogInMessage(string name):base (name)
        {
            
        }

        public override void HandleMessage(RichTextBox textBox, Client c)
        {
            ///checks if a client object was entered,
            ///if so it enters the recieved username to
            ///the entered client.
            ///this onnly occurs on the server side.
            if (c != null)
            {

                c.UserName = username;

            }
            ///informs the client and server that a user 
            ///has logged in through the given textbox
            if (textBox!=null)
            {
                if (textBox.InvokeRequired)
                {
                    MessageInvoker mi = new MessageInvoker(HandleMessage);
                    textBox.Invoke(mi, textBox, c);
                }
                else
                {
                    //finds the end of the chat conversation
                    textBox.SelectionStart = textBox.Text.Length;
                    //prints the time of the message
                    textBox.SelectedText = time.ToShortTimeString();
                    //print the sender and the message
                    textBox.SelectedText = " " + username + " has logged in. \n";
                    //scrolls the text box down
                    textBox.ScrollToCaret();

                    

                }
            }

            
            
        }
    }
}
